package com.desire.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author desire
 */
@Component
public class RoutingConsumer {
    /**
     * 需要设置为手动确认模式
     * 需要消费者使用 basic.reject 或者 basic.back
     */
    @RabbitListener(queues = {"queue"})
    public void handler(Message message, Channel channel) {
        try {
            // 手动拒绝
            // deliveryTag -接收到的AMQP.Basic.GetOk或AMQP.Basic.Deliver
            // requeue -如果被拒绝的消息应该被排队而不是被丢弃/死信，则为true
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

            System.out.println(message.getMessageProperties().toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
